package com.wolf.antlr;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
    }

    static void printFunction(String fileName, List<Tuple<Integer, Integer>> pos) {
        String s;
        try {
            s = Files.readString(Paths.get(fileName));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        for (Tuple<Integer, Integer> tuple : pos) {
            String function = s.substring(tuple.element1(), tuple.element2());
            System.out.println(function);
        }
    }

    static Tuple<String, List<Integer>> parserArgsLines(String[] args) {
        String fileName = args[0];
        List<Integer> integers = Arrays.stream(args).skip(1).mapToInt(Integer::valueOf).boxed().toList();
        return new Tuple<>(fileName, integers);
    }

    static Tuple<String, String> parserArgsDeclaration(String[] args) {
        String fileName = args[0];
        String declaration = Arrays.stream(args).skip(1).collect(Collectors.joining(" "));
        return new Tuple<>(fileName, declaration);
    }

    public static boolean parserArgsIsLine(String[] args) {
        String arg = args[1];
        try {
            Integer.parseInt(arg);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    public static String parserArgs(String[] args, IListener listener) {
        String fileName;
        if (Main.parserArgsIsLine(args)) {
            Tuple<String, List<Integer>> tuple = Main.parserArgsLines(args);
            fileName = tuple.element1();
            List<Integer> integers = tuple.element2();
            listener.setLines(integers);
        } else {
            Tuple<String, String> tuple = Main.parserArgsDeclaration(args);
            fileName = tuple.element1();
            String declaration = tuple.element2();
            listener.setFunctionDeclaration(declaration);
        }
        return fileName;
    }
}